package com.cswangdao.service.impl;

import com.cswangdao.domain.dto.BrandPageDTO;
import com.cswangdao.domain.model.MarketBrand;
import com.cswangdao.mapper.MarketBrandMapper;
import com.cswangdao.result.PageResult;
import com.cswangdao.service.BrandService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class BrandServiceImpl implements BrandService {

    @Autowired
    private MarketBrandMapper brandMapper;


    @Override
    public PageResult<MarketBrand> pageQueryInfo(BrandPageDTO pageDTO) {

        try /*(SqlSession sqlSession = MybatisUtil.getSqlSession(true))*/{
           // MarketBrandMapper brandMapper = sqlSession.getMapper(MarketBrandMapper.class);
            PageHelper.startPage(pageDTO.getPage(), pageDTO.getLimit());
            Page<MarketBrand> brandPage = brandMapper.pageQueryInfo(pageDTO);
            PageResult<MarketBrand> result = PageResult.result(brandPage);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public MarketBrand addBrand(MarketBrand brand) {
        try /*(SqlSession sqlSession = MybatisUtil.getSqlSession();)*/{
            Date now = new Date();
            brand.setAddTime(now);
            brand.setUpdateTime(now);
          //  MarketBrandMapper brandMapper = sqlSession.getMapper(MarketBrandMapper.class);
            brandMapper.addBrand(brand);
           // sqlSession.commit();
            return brand;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean updateById(MarketBrand brand) {
        try/*(SqlSession sqlSession = MybatisUtil.getSqlSession();)*/ {

        //    MarketBrandMapper brandMapper = sqlSession.getMapper(MarketBrandMapper.class);
            brandMapper.updateById(brand);
          //  sqlSession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteById(Integer id) {
        try /*(SqlSession sqlSession = MybatisUtil.getSqlSession();)*/{
         //   MarketBrandMapper brandMapper = sqlSession.getMapper(MarketBrandMapper.class);
            brandMapper.deleteById(id);
         //  sqlSession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
